var goodsCommentsPage = function () {
};
// APP
if (window.plus) {
    goodsCommentsPageReady();
} else {
    document.addEventListener('plusready', goodsCommentsPageReady, false);
}

// Web
$(function () {
    if (AGENT_CLIENT.WEB) {
        goodsCommentsPageReady();
    }
});

// 初始化方法
function goodsCommentsPageReady() {
    // IScroll
    // _loadingStep: 加载状态0默认，1显示加载状态，2执行加载数据，只有当为0时才能再次加载，这是防止过快拉动刷新
    var _myScroll, _pullUpEl, _pullUpL, _pagecount = 1, _loadingStep = 0, _isshowend = false, _isdisplay = false;
    var _goodsId;
    goodsCommentsPage = function () {
    };
    goodsCommentsPage.prototype = {
        // 初始化页面
        init: function (goodsId) {
            _goodsId = goodsId;
            this.paintpage();
            this.loaddata();
            this.bindEvent();
            this.initIscroll();
        },
        //加载商品评论信息
        loaddata: function () {
            var model = this;
            if (_pagecount == 1) {
                _isshowend = false;
                $("#goodsitemlist").empty();
            }
            var postdata = {index: _pagecount, size: 10, query: "", search: {}, sorting: {}};
            var ctype = $("#atypetab li[class=pjhg]").attr("tip");
            postdata.search = {type: ctype};
            // 商品详情
            $.ajax({
                    headers: {'Content-Type': 'application/json'},
                    type: "POST",
                    url: CONFIG_MANAGER.RequstUrl + "v1/goods/comments/" + _goodsId,
                    data: JSON.stringify(postdata),
                    beforeSend: function () {
                        $.showWaiting();
                    }
                }
            ).done(function (res) {
                    var response = JSON.parse(res);
                    var list = response.data.list;
                    if (ctype == "0" && _pagecount == 1) {
                        model.initEnd(response.data)
                        model.loadcount(response.data);
                    }
                    if (list.length < 10) {
                        _isshowend = true;
                    }
                    var html = [];
                    $.each(list, function (i, item) {
                        html.push("<div class='mbbottom sppjbg'><div class='pingjiabg'>");

                        html.push("<p class='pjtx'><span><img src='" + CONFIG_MANAGER.imgeUrl + item.avatar + "'/></span>" + item.member_name + "</p>");
                        html.push("<div class='pjxx comment-list'><em style='width:" + (item.grade / 5 * 100) + "%'></em></div></div>");

                        html.push("<div class='sppjk'>");
                        html.push("<p class='spjnr'>" + item.content + "</p>");
                        html.push("<p class='xinghao'><span>" + new Date(item.create_time_text).Format('yyyy-mm-dd') + "</span>" + item.sku_spec + "</p>");
                        html.push("<p class='pjsdtu'>");
                        var piclist = item.picture.split('|');
                        $.each(piclist, function (pic) {
                            if (pic) {
                                html.push("<img src='" + getImageUrl(pic, 93, 93) + "'/>");
                            }
                        });
                        html.push("</p></div></div>");
                    });

                    $("#goodsitemlist").append(html.join(""));
                }
            ).always(function () {
                $.closeWaiting();
            });
        },

        // 加载评论数目
        loadcount: function (data) {
            var tab = $("#atypetab li span");
            $(tab[0]).text("(" + data.all_count + ")");
            $(tab[1]).text("(" + data.good_count + ")");
            $(tab[2]).text("(" + data.medium_count + ")");
            $(tab[3]).text("(" + data.bad_count + ")");
            $(tab[4]).text("(" + data.pic_count + ")");
        },

        //  画评论页面
        paintpage: function () {
            var html = [];
            html.push(" <span id='goodscommentspage' style='display:none;'>");
            html.push("<div class='header' style='top: 0px;'><span class='headleft' id='appraiseback'><a class='back'><i class='iconfont icon-jiantou'></i></a></span><span class='tbbiaoti'>商品评论</span><span class='headright'></span></div>");
            html.push("<div class='tabdiv' style='top: 2px'><ul class='mbbottom qjbt bgbai' id='atypetab'>");
            html.push("<li class='pjhg' tip='0'>全部<br/><span>(20)</span></li>");
            html.push("<li tip='1'>好评<br/><span>(20)</span></li>");
            html.push("<li tip='2'>中评<br/><span>(20)</span></li>");
            html.push("<li tip='3'>差评<br/><span>(20)</span></li>");
            html.push("<li tip='4'>有图<br/><span>(20)</span></li>");
            html.push("</ul></div>");

            html.push("<div class='main'>");
            html.push("<div id='wrapper' style='top:90px;'><div id='scroller'>");
            html.push("<div id='goodsitemlist' style='display: inline-block;width: 100%'></div>");
            html.push("<div id='pullUp' style='display: none;'><span class='pullUpIconloading'></span><span class='pullUpLabel'>努力加载中...</span></div>");
            html.push("</div></div>");
            html.push("<div id='messageinfo' style=' margin-top: 60%;text-align: center;display:none'>暂无数据</div>");

            html.push("</div>");
            html.push(" </span>");
            $("body").append(html.join(""));
        },

        // 绑定事件
        bindEvent: function () {
            var model = this;
            //回退
            $("#appraiseback").on("click", function () {
                model.back();
            });

            // 评价页面
            $("#atypetab li").on("click", function () {
                var tip = $(this).attr("tip");
                var activeli = $('#atypetab li[class=pjhg]');
                if (activeli.attr("tip") == tip) {
                    return;
                }
                activeli.removeClass("pjhg");
                $(this).addClass("pjhg");
                _pagecount = 1;
                model.loaddata();
            });
        },

        // 初始化Iscroll
        initIscroll: function () {
            var model = this;
            _pullUpEl = $('#pullUp');
            _pullUpL = _pullUpEl.find('.pullUpLabel');

            _myScroll = new IScroll('#wrapper', {
                probeType: 2,
                scrollbars: true,
                mouseWheel: false,
                fadeScrollbars: true,
                bounce: true,
                interactiveScrollbars: false,
                shrinkScrollbars: 'scale',
                click: true,
                tap: true,
                keyBindings: true,
                momentum: true
            });
            //滚动时
            _myScroll.on('scroll', function () {

            });
            //滚动完毕
            _myScroll.on('scrollEnd', function () {
                if (this.y <= 5 && this.y < (this.maxScrollY + 10)) {
                    if (!_isshowend) {
                        pullUpAction();
                    }
                }

                $('img.lazy').lazyload({
                    placeholder: "../../images/grey.gif",
                    container: $("#wrapper"),
                    threshold: 100,
                    failure_limit: 10
                });
            });

            // 上拉显示更多
            function pullUpAction() {
                setTimeout(function () {
                    _pagecount = _pagecount + 1;
                    model.loaddata();
                    _pullUpEl.removeClass('loading');
                    _pullUpL.html('上拉显示更多...');
                    _myScroll.refresh();
                    _loadingStep = 0;
                }, 1000);
            }

            document.addEventListener('touchmove', function (e) {
                if (_isdisplay) {
                    e.preventDefault();
                }
            }, false);
        },

        // 初始化完方法
        initEnd: function (data) {

        },

        // 显示
        show: function () {
            _isdisplay = true;
            $("#goodscommentspage").show();
        },

        // 隐藏
        hide: function () {
            _isdisplay = false;
            $("#goodscommentspage").hide();
        },

        // 返回
        back: function () {
            this.hide();
        }
    };
}
